import { h, reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { useSubTable } from './products';
import axios, { Axios } from 'axios';

export function useMethods(status) {
  const columns = [
    {
      field: 'date',
      label: '业务日期',
      type: 'range-picker'
    },
    {
      field: 'bizType',
      label: '业务类别',
      type: 'select'
    },
    { field: 'bizNo', label: '业务单号' },
    {
      field: 'supplierId',
      label: '供应商',
      type: 'select'
    }
  ];

  const router = useRouter();
  const table = ref();

  const tableConfig = reactive({
    operates: {
      operate: false,
      switch: false
    },
    fixed: [3, 1],
    table: {
      toolbarConfig: { search: false },
      forms: columns,
      query: (rest, form) => {
        // console.log({ ...rest }, status.value, form.date?.[0]);
        // return Promise.resolve({ data: [] });
        return axios
          .post('/spd-server/inbounds/queryListPage', { ...rest, ...form })
          .then((res) => {
            console.log(res);
            return res.data;
          });
      },
      columns: [
        { width: 60, field: 'status.desc', title: '状态', fixed: 'left' },
        {
          width: 160,
          field: 'bizNo',
          title: '业务单号',
          fixed: 'left'
        },
        { width: 100, field: 'bizType.desc', title: '业务类别' },
        { width: 100, field: 'materialType.desc', title: '物资大类' },
        { width: 100, field: 'createTime', title: '业务时间' },
        { width: 100, field: 'apNo', title: '会计期间' },
        { width: 100, field: 'supplierId', title: '供应商' },
        {
          width: 100,
          field: 'isFirstAuthorize',
          title: '是否首营企业',
          formatter: ({ row }) => (row.isFirstAuthorize ? '是' : '否')
        },
        { width: 100, field: 'deliveryNo', title: '送货单号' },
        { width: 100, field: 'invoiceMode.desc', title: '票据模式' },
        { width: 100, field: 'invoiceNo', title: '发票号' },
        { width: 100, field: 'invoiceAmt', title: '发票金额', type: 'money' },
        { width: 100, field: 'directWarehsId', title: '直发科室/库房' },
        { width: 100, field: 'purchId', title: '采购单号' },
        { width: 100, field: 'procurementBy', title: '采购人' },
        { width: 100, field: 'procurementDate', title: '采购时间' },
        { width: 100, field: 'createBy', title: '创建人' },
        { width: 100, field: 'createTime', title: '创建时间' },
        { width: 100, field: 'implementBy', title: '验收人' },
        { width: 100, field: 'implementDate', title: '验收时间' },
        { width: 100, field: 'rejectBy', title: '退货人' },
        { width: 100, field: 'rejectDate', title: '退货时间' },
        { width: 100, field: 'checkBy', title: '审核人' },
        {
          title: '操作',
          type: 'operate',
          width: 130,
          params: [
            {
              label: '提交',
              popconfirmTitle: '确认要提交该记录吗？',
              show: (row) => ['1'].includes(row.status.code)
            },
            {
              label: '编辑',
              show: (row) => ['1'].includes(row.status.code)
            },
            {
              label: '删除',
              popconfirmTitle: '确认要删除该记录吗？',
              show: (row) => ['1'].includes(row.status.code)
            },
            {
              label: '验收',
              click: (row) =>
                router.push({
                  path: '/stock-entry-audit-index',
                  query: { id: row.bizId }
                }),
              show: (row) => ['2'].includes(row.status.code) && row.bizType.code != '15'
            },
            // {
            //   label: '审核',
            //   click: () => {},
            //   show: (row) => ['2'].includes(row.status.code)
            // },
            {
              label: '作废',

              show: (row) => ['2'].includes(row.status.code)
            },
            // {
            //   label: '取消验收',
            //   click: () => {},
            //   show: (row) => ['3'].includes(row.status.code)
            // },
            // {
            //   label: '取消审核',
            //   click: () => {},
            //   show: (row) => ['3'].includes(row.status.code)
            // },

            {
              label: '详情'
            }
          ]
        }
      ]
    },
    sub: useSubTable()
  });
  function getSelectionKeys(checkedRows, status: number) {
    return checkedRows.value.filter((item) => {
      return item.status.code == status;
    });
  }

  const disableDel = (checkedRows) => getSelectionKeys(checkedRows, 1).length == 0;
  const disableSubmit = (checkedRows) => getSelectionKeys(checkedRows, 1).length == 0;

  async function doActionByType<T extends (data) => Promise<any>>(rows: any, actions: any) {
    const values = rows.reduce(
      (a, b) => {
        if ((b.bizType as any).code == '15') {
          a.refund.push(b.bizId);
        } else {
          a.entry.push(b.bizId);
        }
        return a;
      },
      { refund: [], entry: [] }
    );

    await Promise.all(
      Object.keys(actions)
        .filter((key) => values[key].length > 0)
        .map((key) => {
          return actions[key](values[key]);
        })
    );
    table.value.refresh();
  }
  async function batchDel(rows) {
    rows = getSelectionKeys(rows, 1);
    await doActionByType(rows, { a: 1 });
  }

  async function batchSubmit(rows) {
    rows = getSelectionKeys(rows, 1);
    await doActionByType(rows, { a: 1 });
  }
  return {
    columns,
    tableConfig,
    table,
    batchDel,
    batchSubmit,
    disableDel,
    disableSubmit
  };
}
